package edu.uoc.tds.pac4.client.ss4;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import edu.uoc.tds.pac4.beans.Event;
import edu.uoc.tds.pac4.beans.Participacio;
import edu.uoc.tds.pac4.beans.Participant;
import edu.uoc.tds.pac4.beans.TipusEvent;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.client.ss4.GestorPantallaSS4;
import edu.uoc.tds.pac4.client.ss4.Helpers;
import edu.uoc.tds.pac4.client.ss4.JPanelEvents;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

public class PantallaInscripcioEvent implements PanelClientInterface {
	private static final long serialVersionUID = 1L;
	protected static final int defaultHeight = 20;
	private JPanel jPanelContainer = null;
	private JPanel jPanelTitol = null;
	private JLabel jLabelTitolUsuari = null;
	private JLabel jLabelNomUsuari = null;
	private JLabel jLabelTitolCodi = null;
	private JLabel jLabelConteBanc = null;
	private JPanel jPanelDadesEvent = null;
	private JLabel jLabelEventTitol = null;
	private JLabel jLabelNomEvent = null;
	private JLabel jLabelTextFieldNomEvent = null;
	private JComboBox jComboBoxTipus = null;
	private JLabel jLabelTipus = null;
	private JLabel jLabelPlaces = null;
	private JLabel jLabelPreu = null;
	private JLabel jLabelTextFieldPlaces = null;
	private JLabel jLabelTextFieldPreu = null;
	private JLabel jLabelTitolDates = null;
	private JLabel jLabelDataInici = null;
	private JLabel jLabelTextFieldDataInici = null;
	private JLabel jLabelDataFi = null;
	private JLabel jLabelTextFieldDataFi = null;
	private JLabel jLabelLlindar = null;
	private JLabel jLabelDurada = null;
	private JLabel jLabelTextFieldLlindar = null;
	private JLabel jLabelTextFieldDurada = null;
	private JLabel jLabelTitolPeriode = null;
	private JPanelEvents jPanelEvents = null;	
	private JPanel jPanelAccionsEvent = null;
	private JButton jButtonGuardarEvent = null;
	private JButton jButtonCancelar = null;
	private Vector<Event> preRequisits = new Vector<Event>();
	private JLabel jLabelTextFieldDataPeriodeInici = null;
	private JLabel jLabelTextFieldDataPeriodeFi = null;
	private JLabel jLabelDataPeriodeInici = null;
	private JLabel jLabelDataPeriodeFi = null;
	private Event event = null;
	private final Dimension dimButton = new Dimension(125, 30);
	private RemoteInterface remot;
	private GestorPantallaSS4 gestor = null;
	private int t=0;
	private int x=0;
	private int c=0;
	private Participacio participacio = null;
	private Participant participant = null;

	public PantallaInscripcioEvent() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Constructor per a modificar un event donat
	 * @param remote
	 * @param event
	 */
	public PantallaInscripcioEvent(RemoteInterface remot, GestorPantallaSS4 gestor, Event event) {
		super();
		this.remot = remot;
		this.gestor = gestor;
		this.event = event;
	}

	/**
	 * Inicialitza la pantalla carregant les dades als camps
	 */
	private void initializeDades() {
		// Mostra les dades de l'event i/o centre
		if(event != null) {
			DateFormat fmt  = new SimpleDateFormat("dd/MM/yyyy");

			jLabelTextFieldNomEvent.setText(event.getDescripcio());
			//Obtenir dades per calcular places disponibles REALS
			t=event.getCapacitat();
			try {
				x=remot.UsuarisInscrits(event.getCodi(), false);				
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}									
			// Es mostres les places disponibles reals
			jLabelTextFieldPlaces.setText(String.valueOf(t-x));
			jLabelTextFieldPreu.setText(String.valueOf(event.getPreu()));
			jLabelTextFieldDataInici.setText(fmt.format(event.getDataInici()));
			jLabelTextFieldDataFi.setText(fmt.format(event.getDataFi()));
			jLabelTextFieldDurada.setText(String.valueOf(event.getDurada()));
			jLabelTextFieldLlindar.setText(String.valueOf(event.getPercentatge()));
			jLabelTextFieldDataPeriodeInici.setText(fmt.format(event.getInscripInici()));
			jLabelTextFieldDataPeriodeFi.setText(fmt.format(event.getInscripFi()));

			participant = gestor.getParticipant();
			jLabelNomUsuari.setText(participant.getNom()+" "+ participant.getCognoms());
			jLabelConteBanc.setText(gestor.getParticipant().getIban());
			try {
				preRequisits = remot.getPrerequisitsEvent(event);
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}
			for(int i=0;i<jComboBoxTipus.getItemCount();i++)
				if(jComboBoxTipus.getItemAt(i).toString().equals(event.getTipusevent().getNom())) {
					jComboBoxTipus.setSelectedIndex(i);
					jComboBoxTipus.setEnabled(false);
					break;
				}
		}
		updatePreRequisits();
	}

	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			Insets defaultInset = new Insets(10, 10, 10, 10);

			GridBagConstraints gbcPanelEvents = new GridBagConstraints();
			gbcPanelEvents.gridx = 0;
			gbcPanelEvents.gridy = 4;
			gbcPanelEvents.weightx = 1;
			gbcPanelEvents.weighty = 1;
			gbcPanelEvents.gridwidth = 3;
			gbcPanelEvents.fill = GridBagConstraints.BOTH;
			gbcPanelEvents.insets = defaultInset;

			JLabel jLabelEvents = new JLabel();
			jLabelEvents.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.PrePossibles"));
			GridBagConstraints gbcLabelEvents = new GridBagConstraints();
			gbcLabelEvents.gridx = 0;
			gbcLabelEvents.gridy = 3;
			gbcLabelEvents.gridwidth = 3;

			GridBagConstraints gbcPanelDadesEvent = new GridBagConstraints();
			gbcPanelDadesEvent.gridx = 4;
			gbcPanelDadesEvent.gridy = 3;
			gbcPanelDadesEvent.gridheight = 8;
			gbcPanelDadesEvent.anchor = GridBagConstraints.PAGE_START;
			gbcPanelDadesEvent.insets = defaultInset;

			GridBagConstraints gbcPanelTitol = new GridBagConstraints();
			gbcPanelTitol.gridx = 0;
			gbcPanelTitol.gridy = 0;
			gbcPanelTitol.gridwidth = 3;
			gbcPanelTitol.gridheight = 3;
			gbcPanelTitol.fill = GridBagConstraints.HORIZONTAL;
			gbcPanelTitol.anchor = GridBagConstraints.PAGE_START;
			gbcPanelTitol.insets = defaultInset;

			jPanelContainer = new JPanel();
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.add(getJPanelTitol(), gbcPanelTitol);
			jPanelContainer.add(getJPanelDadesEvent(), gbcPanelDadesEvent);

			jPanelContainer.add(getJPanelEvents(), gbcPanelEvents);
			jPanelContainer.add(jLabelEvents, gbcLabelEvents);			
			// carrega les dades del event i/o centre
			initializeDades();
		}
		return jPanelContainer;
	}

	/**
	 * This method initializes jPanelEvents
	 * 
	 * @return JPanelEvents
	 */
	private JPanelEvents getJPanelEvents() {
		if (jPanelEvents == null) {
			jPanelEvents = new JPanelEvents();
		}
		return jPanelEvents;
	}

	/**
	 * This method initializes jPanelTitol
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelTitol() {
		if (jPanelTitol == null) {
			Insets titleInset = new Insets(10, 15, 10, 10);
			Insets textInset = new Insets(10, 5, 10, 25);

			jLabelConteBanc = new JLabel("");
			GridBagConstraints gbcConteBanc = new GridBagConstraints();
			gbcConteBanc.gridx = 3;
			gbcConteBanc.gridy = 0;
			gbcConteBanc.insets = textInset;

			jLabelTitolCodi = new JLabel();
			jLabelTitolCodi.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Iban"));
			GridBagConstraints gbcTitolCodi = new GridBagConstraints();
			gbcTitolCodi.gridx = 2;
			gbcTitolCodi.gridy = 0;
			gbcTitolCodi.insets = titleInset;

			jLabelNomUsuari = new JLabel("");
			GridBagConstraints gbcNomUsuari = new GridBagConstraints();
			gbcNomUsuari.gridx = 1;
			gbcNomUsuari.gridy = 0;
			gbcNomUsuari.insets = textInset;

			jLabelTitolUsuari = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Usuari"));
			GridBagConstraints gbcTitolUsuari = new GridBagConstraints();
			gbcTitolUsuari.gridx = 0;
			gbcTitolUsuari.gridy = 0;
			gbcTitolUsuari.insets = titleInset;

			jPanelTitol = new JPanel();
			jPanelTitol.setLayout(new GridBagLayout());
			jPanelTitol.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			jPanelTitol.add(jLabelTitolUsuari, gbcTitolUsuari);
			jPanelTitol.add(jLabelNomUsuari, gbcNomUsuari);
			jPanelTitol.add(jLabelTitolCodi, gbcTitolCodi);
			jPanelTitol.add(jLabelConteBanc, gbcConteBanc);
		}
		return jPanelTitol;
	}

	/**
	 * This method initializes jPanelEvent
	 * Aquest JPanel recull els detalls de l'event 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelDadesEvent() {
		if (jPanelDadesEvent == null) {
			Insets defaultInset = new Insets(10, 5, 10, 5);
			GridBagConstraints gbcPanelAccionsEvent = new GridBagConstraints();
			gbcPanelAccionsEvent.gridx = 0;
			gbcPanelAccionsEvent.gridy = 10;
			gbcPanelAccionsEvent.gridwidth = 4; 
			gbcPanelAccionsEvent.insets = defaultInset;

			// Periode
			GridBagConstraints gbcTextDataPeriodeFi = new GridBagConstraints();
			gbcTextDataPeriodeFi.gridx = 3;
			gbcTextDataPeriodeFi.gridy = 9;
			gbcTextDataPeriodeFi.insets = defaultInset;

			GridBagConstraints gbcLabelDataPeriodeFi = new GridBagConstraints();
			gbcLabelDataPeriodeFi.gridx = 2;
			gbcLabelDataPeriodeFi.gridy = 9;
			gbcLabelDataPeriodeFi.insets = defaultInset;
			gbcLabelDataPeriodeFi.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextDataPeriodeInici = new GridBagConstraints();
			gbcTextDataPeriodeInici.gridx = 1;
			gbcTextDataPeriodeInici.gridy = 9;
			gbcTextDataPeriodeInici.insets = defaultInset;

			GridBagConstraints gbcLabelDataPeriodeInici = new GridBagConstraints();
			gbcLabelDataPeriodeInici.gridx = 0;
			gbcLabelDataPeriodeInici.gridy = 9;
			gbcLabelDataPeriodeInici.insets = defaultInset;
			gbcLabelDataPeriodeInici.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcLabelTitolPeriode = new GridBagConstraints();
			gbcLabelTitolPeriode.gridwidth = 4;
			gbcLabelTitolPeriode.gridx = 0;
			gbcLabelTitolPeriode.gridy = 8;
			gbcLabelTitolPeriode.insets = defaultInset;

			// Dates Event
			GridBagConstraints gbcTextLLindar = new GridBagConstraints();
			gbcTextLLindar.gridx = 3;
			gbcTextLLindar.gridy = 7;
			gbcTextLLindar.insets = defaultInset;

			GridBagConstraints gbcLabelLlindar = new GridBagConstraints();
			gbcLabelLlindar.gridx = 2;
			gbcLabelLlindar.gridy = 7;
			gbcLabelLlindar.insets = defaultInset;
			gbcLabelLlindar.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextDurada = new GridBagConstraints();
			gbcTextDurada.gridx = 1;
			gbcTextDurada.gridy = 7;
			gbcTextDurada.insets = defaultInset;

			GridBagConstraints gbcLabelDurada = new GridBagConstraints();
			gbcLabelDurada.gridx = 0;
			gbcLabelDurada.gridy = 7;
			gbcLabelDurada.insets = defaultInset;
			gbcLabelDurada.anchor = GridBagConstraints.EAST;
			// Dates Event
			GridBagConstraints gbcTextDataFi = new GridBagConstraints();
			gbcTextDataFi.gridx = 3;
			gbcTextDataFi.gridy = 6;
			gbcTextDataFi.insets = defaultInset;

			GridBagConstraints gbcLabelDataFi = new GridBagConstraints();
			gbcLabelDataFi.gridx = 2;
			gbcLabelDataFi.gridy = 6;
			gbcLabelDataFi.insets = defaultInset;
			gbcLabelDataFi.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextDataInici = new GridBagConstraints();
			gbcTextDataInici.gridx = 1;
			gbcTextDataInici.gridy = 6;
			gbcTextDataInici.insets = defaultInset;

			GridBagConstraints gbcLabelDataInici = new GridBagConstraints();
			gbcLabelDataInici.gridx = 0;
			gbcLabelDataInici.gridy = 6;
			gbcLabelDataInici.insets = defaultInset;
			gbcLabelDataInici.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcLabelTitolDates = new GridBagConstraints();
			gbcLabelTitolDates.gridwidth = 4;
			gbcLabelTitolDates.gridx = 0;
			gbcLabelTitolDates.gridy = 5;
			gbcLabelTitolDates.insets = defaultInset;
			// Preu
			GridBagConstraints gbcTextPreu = new GridBagConstraints();
			gbcTextPreu.gridx = 2;
			gbcTextPreu.gridy = 4;
			gbcTextPreu.insets = defaultInset;
			gbcTextPreu.anchor = GridBagConstraints.WEST;

			GridBagConstraints gbcLabelPreu = new GridBagConstraints();
			gbcLabelPreu.gridx = 1;
			gbcLabelPreu.gridy = 4;
			gbcLabelPreu.insets = defaultInset;
			gbcLabelPreu.fill = GridBagConstraints.BOTH;
			// Places Reals Disponibles
			GridBagConstraints gbcTextPlaces = new GridBagConstraints();
			gbcTextPlaces.gridx = 2;
			gbcTextPlaces.gridy = 3;
			gbcTextPlaces.insets = defaultInset;
			gbcTextPlaces.anchor = GridBagConstraints.WEST;

			GridBagConstraints gbcLabelPlaces = new GridBagConstraints();
			gbcLabelPlaces.gridx = 1;
			gbcLabelPlaces.gridy = 3;
			gbcLabelPlaces.insets = defaultInset;
			gbcLabelPlaces.fill = GridBagConstraints.BOTH;
			

			// Tipus
			GridBagConstraints gbcLabelTipus = new GridBagConstraints();
			gbcLabelTipus.gridx = 0;
			gbcLabelTipus.gridy = 2;
			gbcLabelTipus.insets = defaultInset;
			gbcLabelTipus.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcComboTipus = new GridBagConstraints();
			gbcComboTipus.gridx = 1;
			gbcComboTipus.gridy = 2;
			gbcComboTipus.gridwidth = 3;
			gbcComboTipus.insets = defaultInset;
			// Nom
			GridBagConstraints gbcTextNom = new GridBagConstraints();
			gbcTextNom.gridx = 1;
			gbcTextNom.gridy = 1;
			gbcTextNom.gridwidth = 3;
			gbcTextNom.insets = defaultInset;
			gbcTextNom.fill = GridBagConstraints.BOTH;

			GridBagConstraints gbcLabelNom = new GridBagConstraints();
			gbcLabelNom.gridx = 0;
			gbcLabelNom.gridy = 1;
			gbcLabelNom.insets = defaultInset;
			gbcLabelNom.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcLabelTitol = new GridBagConstraints();
			gbcLabelTitol.gridwidth = 4;
			gbcLabelTitol.gridx = 0;
			gbcLabelTitol.gridy = 0;
			gbcLabelTitol.insets = defaultInset;

			jLabelEventTitol = new JLabel();
			jLabelEventTitol.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Dades"));
			jLabelNomEvent = new JLabel();
			jLabelNomEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Nom"));
			jLabelTextFieldNomEvent = new JLabel();
			jLabelTipus = new JLabel();
			jLabelTipus.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Tipus"));
			jLabelPlaces = new JLabel();
			jLabelPlaces.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Places"));
			jLabelPreu = new JLabel();
			jLabelPreu.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Preu"));
			jLabelTitolDates = new JLabel();
			jLabelTitolDates.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Dates"));
			jLabelDataInici = new JLabel();
			jLabelDataInici.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Inici"));
			jLabelDataFi = new JLabel();
			jLabelDataFi.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Fi"));
			jLabelLlindar = new JLabel();
			jLabelLlindar.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Llindar"));
			jLabelDurada = new JLabel();
			jLabelDurada.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Durada"));
			jLabelTitolPeriode = new JLabel();
			jLabelTitolPeriode.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Periode"));
			jLabelDataPeriodeInici = new JLabel();
			jLabelDataPeriodeInici.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Inici"));
			jLabelDataPeriodeFi = new JLabel();
			jLabelDataPeriodeFi.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Fi"));
			jPanelDadesEvent = new JPanel();
			jPanelDadesEvent.setLayout(new GridBagLayout());
			jPanelDadesEvent.add(jLabelEventTitol, gbcLabelTitol);
			jPanelDadesEvent.add(jLabelNomEvent, gbcLabelNom);
			jPanelDadesEvent.add(jLabelTextFieldNomEvent, gbcTextNom);
			jPanelDadesEvent.add(jLabelTipus, gbcLabelTipus);
			jPanelDadesEvent.add(getJComboBoxTipus(), gbcComboTipus);
			jPanelDadesEvent.add(jLabelPlaces, gbcLabelPlaces);
			jLabelTextFieldPlaces = new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldPlaces, gbcTextPlaces);
			jPanelDadesEvent.add(jLabelPreu, gbcLabelPreu);
			jLabelTextFieldPreu = new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldPreu, gbcTextPreu);
			jPanelDadesEvent.add(jLabelTitolDates, gbcLabelTitolDates);
			jPanelDadesEvent.add(jLabelDataInici, gbcLabelDataInici);
			jLabelTextFieldDataInici = new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldDataInici, gbcTextDataInici);
			jPanelDadesEvent.add(jLabelDataFi, gbcLabelDataFi);
			jLabelTextFieldDataFi = new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldDataFi, gbcTextDataFi);
			jPanelDadesEvent.add(jLabelDurada, gbcLabelDurada);
			jLabelTextFieldDurada = new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldDurada, gbcTextDurada);
			jPanelDadesEvent.add(jLabelLlindar, gbcLabelLlindar);
			jLabelTextFieldLlindar = new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldLlindar, gbcTextLLindar);
			jPanelDadesEvent.add(jLabelTitolPeriode, gbcLabelTitolPeriode);
			jPanelDadesEvent.add(jLabelDataPeriodeInici, gbcLabelDataPeriodeInici);
			jLabelTextFieldDataPeriodeInici= new JLabel();
			jPanelDadesEvent.add(jLabelTextFieldDataPeriodeInici, gbcTextDataPeriodeInici);
			jLabelTextFieldDataPeriodeFi= new JLabel();
			jPanelDadesEvent.add(jLabelDataPeriodeFi, gbcLabelDataPeriodeFi);
			jPanelDadesEvent.add(jLabelTextFieldDataPeriodeFi, gbcTextDataPeriodeFi);
			jPanelDadesEvent.add(getJPanelAccionsEvent(), gbcPanelAccionsEvent);
		}
		return jPanelDadesEvent;
	}

	/**
	 * This method initializes jPanelAccions	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelAccionsEvent() {
		if (jPanelAccionsEvent == null) {
			Insets defaultInset = new Insets(10, 10, 10, 10);

			// Botons
			GridBagConstraints gbcButtonGuardar = new GridBagConstraints();
			gbcButtonGuardar.gridy = 0;
			gbcButtonGuardar.insets = defaultInset;

			GridBagConstraints gbcButtonCancelar = new GridBagConstraints();
			gbcButtonCancelar.gridx = 1;
			gbcButtonCancelar.gridy = 0;
			gbcButtonCancelar.insets = defaultInset;

			jPanelAccionsEvent = new JPanel();
			jPanelAccionsEvent.setLayout(new GridBagLayout());
			jPanelAccionsEvent.add(getJButtonGuardarEvent(), gbcButtonGuardar);
			jPanelAccionsEvent.add(getJButtonCancelar(), gbcButtonCancelar);
		}
		return jPanelAccionsEvent;
	}


	/**
	 * This method initializes jButtonGuardarEvent
	 * @return
	 */
	private JButton getJButtonCancelar() {
		if(jButtonCancelar == null) {
			jButtonCancelar = new JButton();
			jButtonCancelar.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Cancelar"));
			jButtonCancelar.setPreferredSize(dimButton);
			jButtonCancelar.setMinimumSize(dimButton);
			jButtonCancelar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {				
					gestor.carregaPantalla(GestorPantallaSS4.TipusPantalla.consultarEvent, null);
				}
			});
		}
		return jButtonCancelar;
	}

	/**
	 * This method initializes jButtonGuardarEvent
	 * @return
	 */
	private JButton getJButtonGuardarEvent() {
		if(jButtonGuardarEvent == null) {
			jButtonGuardarEvent = new JButton();
			jButtonGuardarEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaInscripcioEvent.Inscriure"));
			jButtonGuardarEvent.setPreferredSize(dimButton);
			jButtonGuardarEvent.setMinimumSize(dimButton);
			jButtonGuardarEvent.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {

					if(creacioInscripcio())
						try {								
							remot.insertarParticipacio(participacio);
							new PantallaError("Client.PantallaInscripcioEvent.InscripcioOK", PantallaError.Tipus.INFO);

							//torna al llistat d'events
							gestor.carregaPantalla(GestorPantallaSS4.TipusPantalla.consultarEvent, null);
						} catch (RemoteException e) {
							new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
						} catch (GestioEventsException e) {
							new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
						} 
				}
			});
		}
		return jButtonGuardarEvent;
	}

	/**
	 * Verifica que es cumpleixen els requisits
	 * abans de realitzar la inscripcio
	 * @return boolean
	 */

	protected boolean creacioInscripcio() {

		if((t-x)==0) {
			new PantallaError("Client.PantallaInscripcioEvent.PlacesKO", PantallaError.Tipus.ERROR);
			return false;
		} else {
			Date currentDate = new Date();
			Date inici = event.getInscripInici();
			if (currentDate.before(inici)) {
				new PantallaError("Client.PantallaInscripcioEvent.IniciInsKO", PantallaError.Tipus.ERROR);
				return false;
			} else {
				Date fi = event.getInscripFi();
				if (currentDate.after(fi)){
					new PantallaError("Client.PantallaInscripcioEvent.FiInsKO", PantallaError.Tipus.ERROR);
					return false;
				}
			}
		}
		//Comprova que cumpleix prerrequisits events
		if (!cumpleixPreRequisits()){
			new PantallaError("Client.PantallaInscripcioEvent.PreReqKO", PantallaError.Tipus.ERROR);
			return false;
		}
		
		// Crea participacio
		participacio = new Participacio();

		participacio.setPartipant(participant);
		participacio.setCodi_event(event.getCodi());
		participacio.setAssistencia(false);

		return true;
	}
	/**
	 * Comprova si el Participant ha participat
	 * en els events prerequerits (amb assistencia confirmada)
	 * @return
	 */
	protected boolean cumpleixPreRequisits(){
		boolean cumpleix = false;
		try {
			c=0;
			cumpleix = false;
			for(int i=0;i<preRequisits.size();i++){
				if(remot.Participa(participant.getCodi(), preRequisits.get(i).getCodi(), true)){
					c=c+1;
				}
			}
			if (c==preRequisits.size()){
				cumpleix=true;
			} 			
		} catch (RemoteException e) {
			new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
		} catch (GestioEventsException e) {
			new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
		} 
		return cumpleix;
	}

	/**
	 * This method initializes jComboBoxTipus
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxTipus() {
		if (jComboBoxTipus == null) {
			jComboBoxTipus = new JComboBox();
			jComboBoxTipus.setMinimumSize(new Dimension(200, defaultHeight));
			jComboBoxTipus.setPreferredSize(new Dimension(200, defaultHeight));
			Vector<TipusEvent> tipusEvents = null;
			try {
				tipusEvents = remot.getTipusEvents(true);
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}

			Helpers.setItemsCombo(tipusEvents, jComboBoxTipus);
		}
		return jComboBoxTipus;
	}


	/**
	 * Redibuixa la llista d'events prerequisits
	 */
	private void updatePreRequisits()
	{
		if(preRequisits != null && !preRequisits.isEmpty())
			jPanelEvents.drawEvents(preRequisits);
	}

}